package org.jet.emall;

import org.jet.emall.common.exception.CustomServiceException;
import org.jet.emall.common.restful.result.CommonResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-11 11:03
 */
@RestControllerAdvice
public class ExceptionController {

    /**
     * 根据业务规则,统一处理异常。
     */
    @ExceptionHandler(Exception.class)
    public CommonResult<Object> exceptionHandler(HttpServletRequest request, Throwable error) {
        error.printStackTrace();
        if( error instanceof MethodArgumentNotValidException){
            return CommonResult.badRequest("参数错误");
        }else if (error instanceof CustomServiceException){
            return CommonResult.notFound(error.getMessage());
        }else{
            return CommonResult.serverError(error.getMessage());
        }


    }

}
